package com.un.ebs.sys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.un.ebs.core.exception.BusinessException;
import com.un.ebs.core.util.SecurityUtils;
import com.un.ebs.sys.domain.PageClass;
import com.un.ebs.sys.dto.ObjectParentAndIndexChangeDto;
import com.un.ebs.sys.dto.PageClassAddDto;
import com.un.ebs.sys.dto.PageClassUpdateDto;
import com.un.ebs.sys.mapper.PageClassMapper;
import com.un.ebs.sys.mapper.PageMapper;
import com.un.ebs.sys.service.PageClassService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author liubo
 * @since 2020-04-19
 */
@Service
public class PageClassServiceImpl extends ServiceImpl<PageClassMapper, PageClass> implements PageClassService {

    @Autowired
    private PageMapper pageMapper;

    @Override
    public void add(PageClassAddDto req) {
        PageClass obj = new PageClass();
        obj.setName(req.getName());
        obj.setParentId(req.getParentId());
        if (StringUtils.isEmpty(req.getParentId())) {
            obj.setSort(getBaseMapper().maxSort());
        } else {
            obj.setSort(getBaseMapper().maxSortByParentId(req.getParentId()));
        }
        if (com.un.ebs.core.util.StringUtils.isNullOrEmpty(SecurityUtils.getUserContent().getPersonnelName())) {
            obj.setCreateAtName(SecurityUtils.getUserContent().getUserName());
        } else {
            obj.setCreateAtName(SecurityUtils.getUserContent().getPersonnelName());
        }
        obj.insert();
    }

    @Override
    public void update(PageClassUpdateDto req) {
        PageClass obj = getById(req.getId());
        obj.setName(req.getName());
        obj.setParentId(req.getParentId());
        obj.updateById();
    }

    @Override
    public void delete(String id) {
        LambdaQueryWrapper<PageClass> lq = Wrappers.lambdaQuery();
        lq.eq(PageClass::getParentId, id);
        int cnt = baseMapper.selectCount(lq);
        if (cnt > 0) {
            throw new BusinessException("请先删除子类别。");
        }

        LambdaQueryWrapper<com.un.ebs.sys.domain.Page> query = Wrappers.lambdaQuery();
        query.eq(com.un.ebs.sys.domain.Page::getClassId, id);
        cnt = pageMapper.selectCount(query);
        if (cnt > 0) {
            throw new BusinessException("请先删除类别下的所有 page ");
        }

        baseMapper.deleteById(id);
    }

    @Override
    public PageClass queryById(String id) {
        return getBaseMapper().selectById(id);
    }

    @Override
    public void changeParent(String id, String parentId) {
        PageClass obj = getById(id);
        obj.setParentId(parentId);
        getBaseMapper().updateById(obj);
    }

    @Override
    public List<PageClass> queryByParentId(String parentId) {
        return queryPageByParentId(parentId, 999, 1).getRecords();
    }

    @Override
    public IPage<PageClass> queryPageByParentId(String parentId, int pageSize, int pageIndex) {
        Page<PageClass> page = new Page<>(pageIndex, pageSize);
        LambdaQueryWrapper<PageClass> wrapper = Wrappers.lambdaQuery();
        if (com.un.ebs.core.util.StringUtils.isNullOrEmpty(parentId)) {
            wrapper.isNull(PageClass::getParentId);
        } else {
            wrapper.eq(PageClass::getParentId, parentId);
        }
        return getBaseMapper().selectPage(page, wrapper);
    }

    @Override
    public List<PageClass> queryAll() {
        LambdaQueryWrapper<PageClass> lq = Wrappers.lambdaQuery();
        lq.eq(PageClass::getCreateAt, SecurityUtils.getUserContent().getId());
        return getBaseMapper().selectList(lq);
    }

    @Override
    public void changeSort(String id, int index) {
        PageClass obj = getById(id);
        obj.setSort(index);
        getBaseMapper().updateById(obj);
    }

    @Transactional
    @Override
    public void changeNode(List<ObjectParentAndIndexChangeDto> nodes) {
        for (ObjectParentAndIndexChangeDto node : nodes) {
            PageClass obj = baseMapper.selectById(node.getId());
            obj.setSort(node.getIndex());
            obj.setParentId(node.getParentId());
            obj.updateById();
        }
    }
}